// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import EventMng from "./EventMng";
import { GameEvent } from "./GameEvent";
import { Utils } from "./Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class hallScene extends cc.Component {

    @property(sp.Skeleton)
    skeleton: sp.Skeleton = null;
    @property(cc.Prefab)
    scrollView: cc.Prefab = null;

    atlasStr = 'http://crazyfarm.oss-cn-beijing.aliyuncs.com/1.atlas';
    jsonStr = 'http://crazyfarm.oss-cn-beijing.aliyuncs.com/1.json';
    pngStr = 'http://crazyfarm.oss-cn-beijing.aliyuncs.com/1.png';


    img1 = 'https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fimg.guguzhu.com%252Fd%252Ffile%252Fandroid%252Fico%252F2021%252F07%252F20%252F0bwiccpmvdd.png%26refer%3Dhttp%253A%252F%252Fimg.guguzhu.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1634400235%26t%3Dbfc33d66e602ea02a5cf6fe512ca06f5&thumburl=https%3A%2F%2Fimg1.baidu.com%2Fit%2Fu%3D229153104%2C2495614763%26fm%3D253%26fmt%3Dauto%26app%3D120%26f%3DPNG%3Fw%3D192%26h%3D192'；
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        EventMng.on(GameEvent.ATLAS_ASSET, this.onAtlasCall, this);
        EventMng.on(GameEvent.JSON_ASSET, this.onJsonCall, this);
        EventMng.on(GameEvent.PNG_ASSET, this.onPngCall, this);
        // let node = cc.instantiate(this.scrollView);
        // node.parent = this.node;
        // this.scrollnode.getComponent('verticalSimple').addItem(null);
        // this.node.getComponent('verticalSimple').addItem(null);
    }

    onLoadSkeleton(atlasUrl, jsonUrl, imageUrl) {
        cc.assetManager.loadAny([{ url: atlasUrl, ext: ".txt" }, { url: jsonUrl, ext: ".txt" }], (error, assets) => {
            cc.assetManager.loadRemote(imageUrl, {ext: '.png'}, (error, texture) => {
                var asset = new sp.SkeletonData();
                asset.skeletonJson = assets[1];
                asset.atlasText = assets[0];
                asset.textures = [texture];
                asset.textureNames = ["1.png"];
                this.skeleton.skeletonData = asset;
                this.skeleton.animation = 'left';
            });
        });
    }

    onClickBtn() {
        this.onLoadSkeleton(this.atlasStr, this.jsonStr, this.pngStr);
        // this.onLoadImg( this.pngStr, cc.find('Canvas/spriteNode'))
    }

    onLoadImg(url, asset: cc.Node) {
        cc.assetManager.loadRemote(url, {ext: '.png'}, (error, texture) => {
            var spr = new cc.SpriteFrame(texture);
            asset.getComponent(cc.Sprite).spriteFrame = spr;
         });
    }

    onCloneAsset() {
       
    }

    onAtlasCall(data) {
        this.skeleton.skeletonData = data;
    }

    onJsonCall() {

    }

    onPngCall() {
        
    }

    start() {

    }

    // update (dt) {}
}
